import math

WIDTH = 900
HEIGHT = 900

BACK_COLOR = (127, 0, 127)
CIRCLE_COLOR = (200, 200, 200)

def normalize(x, y):
    s = math.sqrt(x ** 2 + y ** 2)
    return x / s, y / s
class MyCircle:
    def __init__(self, x, y, r, dir_x, dir_y):
        self.x = x
        self.y = y
        self.r = r
        self.dir_x, self.dir_y = normalize(dir_x, dir_y)
        self.spd = 1

    def SetSpeed(self, spd):
        self.spd = spd

    def Update(self):
        self.x += self.dir_x * self.spd
        self.y += self.dir_y * self.spd

        bounce = False
        if self.x + self.r > WIDTH:
            self.x = WIDTH - self.r
            bounce = True
        
        if self.x - self.r < 0:
            self.x = self.r
            bounce = True
        
        if bounce == True:
            self.dir_x *= -1

        bounce = False

        if self.y + self.r > HEIGHT:
            self.y = HEIGHT - self.r
            bounce = True

        if self.y - self.r < 0:
            self.y = self.r
            bounce = True
        
        if bounce == True:
            self.dir_y *= -1
    
    def OnCollide(self, c):
        cdir_x = c.x - self.x
        cdir_y = c.y - self.y
        cdir_x, cdir_y = normalize(cdir_x, cdir_y)
        self.dir_x -= cdir_x
        self.dir_y -= cdir_y

        self.dir_x, self.dir_y = normalize(self.dir_x, self.dir_y)
        

    def Draw(self):
        screen.draw.filled_circle((self.x, self.y), self.r, CIRCLE_COLOR)

def on_key_down(key):
    if key == keys.ESCAPE:
        quit() 
        
def on_mouse_down(pos):
    print("mouse_down", pos)
    
def on_mouse_up(pos):
    print("mouse_up", pos)

c1 = MyCircle(10,  10,  15,  3,  1)
c2 = MyCircle(15,  15,  25,  1,  -2)
c3 = MyCircle(20,  20,  35, -1,  -5)
c4 = MyCircle(25,  25,  45, -3,  -8)
c5 = MyCircle(30,  30,  55, -5, -11)
c6 = MyCircle(35,  35,  65, -7, -14)
c7 = MyCircle(40,  40,  75, -9, -17)
c8 = MyCircle(45,  45,  85, -11,-20)
c9 = MyCircle(50,  50,  95, -13,-23)
cx = MyCircle(55,  55, 105, -15,-26)
c1.SetSpeed(10)
c2.SetSpeed(10)
c3.SetSpeed(10)
c4.SetSpeed(10)
c5.SetSpeed(10)
c6.SetSpeed(10)
c7.SetSpeed(10)
c8.SetSpeed(10)
c9.SetSpeed(10)
cx.SetSpeed(10)
def MakeList(cv,prev_list):
    prev_list.append(cv)
    return prev_list
def CheckCollision(ca, cb):
    if math.sqrt((ca.x - cb.x) ** 2 + (ca.y - cb.y) ** 2) <= ca.r + cb.r:
        ca.OnCollide(cb)
        cb.OnCollide(ca)
def TestMakeList():
    prev_list = []
    prev_list = MakeList(c1,prev_list)
    prev_list = MakeList(c2,prev_list)
    prev_list = MakeList(c3,prev_list)
    prev_list = MakeList(c4,prev_list)
    prev_list = MakeList(c5,prev_list)
    prev_list = MakeList(c6,prev_list)
    prev_list = MakeList(c7,prev_list)
    prev_list = MakeList(c8,prev_list)
    prev_list = MakeList(c9,prev_list)
    prev_list = MakeList(cx,prev_list)
    return prev_list
def update():
    circle_list = TestMakeList()
    for i in range(0,len(circle_list)):
        for j in range(i + 1,len(circle_list)):
            CheckCollision(circle_list[i],circle_list[j])
    c1.Update()
    c2.Update()
    c3.Update()
    c4.Update()
    c5.Update()
    c6.Update()
    c7.Update()
    c8.Update()
    c9.Update()
    cx.Update()
        
def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    c1.Draw()
    c2.Draw()
    c3.Draw()
    c4.Draw()
    c5.Draw()
    c6.Draw()
    c7.Draw()
    c8.Draw()
    c9.Draw()
    cx.Draw()

